import { message } from 'antd';
import axios from 'axios';
import { getToken } from 'utils/storage';
import history from './history';
const baseURL = 'http://geek.itheima.net';
const instance = axios.create({
	baseURL,
	timeout: 5000,
});

// 添加请求拦截器
instance.interceptors.request.use((config) => {
	if (getToken()) {
		config.headers.Authorization = `Bearer ${getToken()}`;
	}
	return config;
});

// 添加响应拦截器
instance.interceptors.response.use(
	(response) => {
		console.log('response------->', response);

		return response.data.data;
	},
	(err) => {
		// console.log(err.response.status);

		// 用于判断token是否失效
		if (err.response.status === 401) {
			message.error(err.response.data.message);
			history.push('/login');
			console.log('12345678------->', 12345678);
			return Promise.reject(err);
		}
	}
);

export default instance;
